<template>
  <div class="app-container">
    <sticky class="filter-container">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-input
            v-model="listSearch.search"
            placeholder="搜索"
            style="margin-right: 10px;width: 200px"
            class="filter-item"
            size="mini"
            clearable
            @keyup.enter.native="handleSearch"
            @clear="handleSearch"
            @blur="handleSearch"
          />
          <el-button
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-search"
            @click="handleSearch"
          >
            搜索
          </el-button>
        </el-col>
        <el-col :span="8" style="text-align: right">
          <el-button
            v-if="checkPermission(['admin','menu_all','menu_create'])"
            v-waves
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="showAdd"
          >
            新增
          </el-button>
        </el-col>
      </el-row>
    </sticky>
    <!-- 表格渲染 -->
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @sort-change="sortChange"
    >
      <el-table-column
        prop="name"
        label="菜单名"
        width="200px"
      />
      <el-table-column prop="icon" label="图标" align="center" width="80px">
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon" />
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        align="center"
        width="80px"
        sortable="custom"
      >
        <template slot-scope="scope">
          <el-tag>{{ scope.row.sort }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" prop="path" label="链接地址" />
      <el-table-column :show-overflow-tooltip="true" prop="component" label="组件路径" />
      <el-table-column prop="is_frame" width="80px" align="center" label="内部菜单">
        <template slot-scope="scope">
          <span v-if="!scope.row.is_frame"><el-tag>是</el-tag></span>
          <span v-else><el-tag type="info">否</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column prop="is_frame" width="85px" align="center" label="是否显示">
        <template slot-scope="scope">
          <span v-if="scope.row.is_show"><el-tag>显示</el-tag></span>
          <span v-else><el-tag type="info">不显示</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="120px"
        align="center"
      >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <el-button
              v-if="checkPermission(['admin','menu_all','menu_list'])"
              type="text"
              icon="el-icon-view"
              @click="showView(scope.row.id)"
            />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="编辑" placement="top">
            <el-button
              v-if="checkPermission(['admin','menu_all','menu_edit'])"
              type="text"
              icon="el-icon-edit"
              :disabled="scope.row.id === 1"
              @click="showEdit(scope.row.id)"
            />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="top">
            <el-button
              v-if="checkPermission(['admin','menu_all','menu_delete'])"
              type="text"
              icon="el-icon-delete"
              :disabled="scope.row.id === 1"
              @click="handleDelete(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      style="text-align: right; margin-top: 20px"
      background
      :hide-on-single-page="true"
      :current-page="listSearch.page"
      :page-size="listSearch.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="listSearch.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 弹框组件 -->
    <vForm :vdialog="dialog" :vtitle="title" :visedit="isedit" :vformid="formid" @changeShow="showUpdate" />
  </div>
</template>

<script>
// 按钮吸顶
import Sticky from '../../../components/Sticky/index'
import waves from '../../../directive/waves/waves'
import vForm from './components/form'
import { listMenu, delMenu } from '@/api/menu'
import checkPermission from '../../../utils/permission'

export default {
  components: { vForm, Sticky },
  directives: { waves },
  data() {
    return {
      listLoading: false,
      list: [],
      listSearch: {
        total: 0,
        page: 1,
        pageSize: 10,
        ordering: 'sort',
        search: ''
      },
      // 弹出框定义属性
      // （与子组件的 props 属性一致）dialog 控制新增、编辑、查看弹窗的显示与隐藏
      dialog: false,
      title: '',
      isedit: '',
      formid: 0
      // 弹出框定义属性
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // checkPermission 权限检查
    checkPermission,
    // 弹出框
    // 点击按钮事件，显示弹窗组件
    showAdd() {
      this.dialog = true
      this.title = '新增菜单'
      this.isedit = 'add'
    },
    showEdit(row) {
      this.dialog = true
      this.title = '编辑菜单'
      this.isedit = 'edit'
      this.formid = row
    },
    showView(row) {
      this.dialog = true
      this.title = '菜单详情'
      this.isedit = 'view'
      this.formid = row
    },
    // 监听 子组件弹窗关闭后触发，由子组件调用
    showUpdate(val, valid) {
      if (val === 'false') {
        this.dialog = false
        this.formid = valid
      } else {
        this.dialog = true
      }
    },
    // 弹出框
    // 列表
    getList() {
      this.listLoading = true
      listMenu(this.listSearch).then(res => {
        this.list = res.data
        this.listSearch.total = res.count
        this.listLoading = false
      })
    },
    // 列表
    // 搜索
    handleSearch() {
      // 当页面不在第一页时搜索内容报错
      this.listSearch.page = 1
      this.getList()
    },
    // 搜索
    // 排序
    sortChange(column) {
      const { prop, order } = column
      if (order === 'descending') {
        this.listSearch.ordering = `-${prop}`
      } else {
        this.listSearch.ordering = prop
      }
      this.handleSearch()
    },
    // 排序
    // 分页
    handleSizeChange(val) {
      this.listSearch.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listSearch.page = val
      this.getList()
    },
    // 分页
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将删除左侧菜单，删除后将不在显示该菜单，建议将"是否显示"项改为"不显示"，是否继续删除【 ' + row.name + ' 】？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.listLoading = true
        delMenu(row.id).then((res) => {
          this.listLoading = false
          this.$notify.success({
            type: 'success',
            title: '成功',
            message: '【 ' + row.name + ' 】菜单已删除',
            showClose: true,
            duration: 5 * 1000
          })
          if (this.listSearch.total % 10 === 1) {
            if (this.listSearch.page !== 1) {
              this.listSearch.page = this.listSearch.page - 1
            }
          }
          this.getList()
        })
      }).catch(() => {
        this.$notify.info({
          type: 'info',
          title: '提示',
          message: '已取消删除',
          showClose: true,
          duration: 5 * 1000
        })
      })
    }
    // 删除
  }
}
</script>

<style scoped>

</style>
